use serde::{Deserialize, Serialize};

use crate::database::{self, table::tag::model::NewTag};

#[derive(Debug, Serialize, Deserialize)]
pub struct TagForm {
    pub id: i32,
    pub name: String,
    pub desc: String,
    pub avatar: String,
    pub slug: String,
    pub cid: i32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NewTagForm {
    pub name: String,
    pub desc: String,
    pub avatar: String,
    pub slug: String,
    pub cid: i32,
}

impl From<&database::table::tag::model::Tag> for TagForm {
    fn from(tag: &database::table::tag::model::Tag) -> Self {
        TagForm {
            id: tag.id,
            name: tag.name.to_string(),
            desc: tag.desc.to_string(),
            avatar: tag.avatar.to_string(),
            slug: tag.slug.to_string(),
            cid: tag.cid,
        }
    }
}

impl TagForm {
    pub fn to(&self) -> NewTagForm {
        NewTagForm {
            name: self.name.to_string(),
            desc: self.desc.to_string(),
            avatar: self.avatar.to_string(),
            slug: self.slug.to_string(),
            cid: self.cid,
        }
    }
}

impl NewTagForm {
    pub fn to(&self) -> NewTag {
        NewTag {
            name: &self.name,
            desc: &self.desc,
            avatar: &self.avatar,
            slug: &self.slug,
            cid: self.cid,
        }
    }
}
